package com.gome.ocean.service.datax.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.gome.ocean.dao.datax.IDataCharsetDAO;
import com.gome.ocean.dao.model.datasource.CharsetRule;
import com.gome.ocean.dao.model.datasource.DataCharset;
import com.gome.ocean.dao.model.websql.DBAllCharsetAndCollation;
import com.gome.ocean.service.datax.IDataCharsetService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * @author zhangguoqing 2016年11月25日 下午2:48:13
 * @since 5.0.0
 */
@Service("iDataCharsetService")
public class DataCharsetServiceImpl implements IDataCharsetService {

    @Autowired
    private IDataCharsetDAO iDataCharsetDao;

    @Override
    public void insert(DataCharset bean) {
        try {
            iDataCharsetDao.insert(bean);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delete(Long id) {
        try {
            iDataCharsetDao.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void update(DataCharset bean) {
        try {
            iDataCharsetDao.update(bean);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public Map<String, Object> getDataCharsetMapByRuleTypeAndDBType(String ruleType, String databaseType,
                                                                    Integer infoType) {
        Map<String, Object> map = Maps.newHashMap();
        try {
            Map<String, Object> paramMap = Maps.newHashMap();
            paramMap.put("ruleType", ruleType);
            paramMap.put("dataBaseType", databaseType);
            paramMap.put("state", infoType);
            DataCharset dataCharset = iDataCharsetDao.getDataCharsetBeanByRuleTypeAndDBType(paramMap);
            if (dataCharset != null) {
                String ruleContent = dataCharset.getRuleContent();
                List<CharsetRule> root = JSONArray.parseArray(ruleContent, CharsetRule.class);
                if (root != null && root.size() > 0) {
                    map.put("root", root);
                    map.put("success", true);
                    map.put("failure", false);
                    return map;
                }
            }
            map.put("root", "");
            map.put("success", true);
            map.put("failure", false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public List<DBAllCharsetAndCollation> getAllCharsetAndCollationsByDBType(String dbType, Integer state) {
        List<DBAllCharsetAndCollation> charsetLists = Lists.newLinkedList();
        List<DataCharset> dataLists = iDataCharsetDao.getListsByDbType(dbType, state);
        if (dataLists != null && dataLists.size() > 0) {
            for (DataCharset dcSet : dataLists) {
                if (dcSet.getRuleContent().startsWith("[{") && dcSet.getRuleContent().endsWith("}]")) {
                    List<CharsetRule> ruleLists = JSONArray.parseArray(dcSet.getRuleContent(), CharsetRule.class);
                    if (ruleLists != null && ruleLists.size() > 0) {
                        for (CharsetRule rule : ruleLists) {
                            DBAllCharsetAndCollation collation = new DBAllCharsetAndCollation();
                            collation.setCharset(dcSet.getRuleType());
                            collation.setCollation(rule.getData());
                            charsetLists.add(collation);
                        }
                    }
                }
            }
        }
        return charsetLists;
    }

}
